/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package questionbook.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import questionbook.Configuration.QuestionbookConfiguration;
import questionbook.exception.IndividualAlreadyExistsException;
import questionbook.model.Person;
import questionbook.model.Question;
import questionbook.model.Suggest;
import questionbook.service.common.GenericServiceImpl;
import questionbook.standard.Qbook;

/**
 *
 * @author stefanstift
 */
public class SuggestServiceBean extends GenericServiceImpl implements SuggestService {

    private PersonService personService = new PersonServiceBean();
    private QuestionService questionService = new QuestionServiceBean();

    public void persist(Suggest suggest) {

        this.storeManager.getStore().add(suggest.getSuggester().getUri(), Qbook.getInstance().getMakesURI(), suggest.getUri());
        this.storeManager.getStore().add(suggest.getUri(), Qbook.getInstance().getForQuestionURI(), suggest.getQuestion().getUri());
        this.storeManager.getStore().add(suggest.getUri(), Qbook.getInstance().getWithPersonURI(), suggest.getSuggested().getUri());

        if(suggest.getText() != null && !suggest.getText().isEmpty())
            this.storeManager.getStore().add(suggest.getUri(), RDFS.LABEL, suggest.getTextLiteral());
    }

    public List<Suggest> getByQuestion(Question q) {

        List<Suggest> suggestList = new ArrayList<Suggest>();

        List<Map> resultList = this.storeManager.getStore().runSPARQL("SELECT ?suggest ?suggester ?suggested ?text"
                + " WHERE { ?suggest <" + RDF.TYPE + "> <" + Qbook.getInstance().getSuggestURI() + "> ."
                + " ?suggest <" + RDFS.LABEL + "> ?text ."
                + " ?suggester <" + Qbook.getInstance().getMakesURI() + "> ?suggest ."
                + " ?suggest <" + Qbook.getInstance().getWithPersonURI() + "> ?suggested ."
                + " ?suggest <" + Qbook.getInstance().getForQuestionURI() + "> <" + q.getUri() + "> "
                + "}");


        for(Map m : resultList)
        {
            Suggest s = new Suggest(((Value) m.get("suggest")).stringValue());
            s.setText(((Value) m.get("text")).stringValue());

            URI suggester = this.storeManager.getStore().getURIref(((Value) m.get("suggester")).stringValue());
            s.setSuggester(this.personService.get(suggester));

            URI suggested = this.storeManager.getStore().getURIref(((Value) m.get("suggested")).stringValue());
            s.setSuggested(this.personService.get(suggested));

            s.setQuestion(q);

            suggestList.add(s);
        }

        return suggestList;
    }

    public void delete(Suggest suggest) {

        this.storeManager.getStore().removeTuplePattern(null, Qbook.getInstance().getMakesURI(), suggest.getUri());
        this.storeManager.getStore().removeTuplePattern(suggest.getUri(), null, null);
        
    }

    public Suggest getIdentifiedSuggest() throws IndividualAlreadyExistsException {
        
        int i = 0;
        String suggestBase = "/qbSuggest";
        Date d = new Date();
        String timestamp = Long.toString(d.getTime());

        String id = QuestionbookConfiguration.getInstance().getBaseURL() + suggestBase + timestamp;
        URI uri = this.storeManager.getStore().getURIref(id);

        List check = this.storeManager.getStore().tuplePattern(uri, null, null);

        while(check != null || !check.isEmpty())
        {
            if(i == 10)
            {
                break;
            }

            d = new Date();
            timestamp = Long.toString(d.getTime());
            id = QuestionbookConfiguration.getInstance().getBaseURL() + suggestBase + timestamp;
            uri = this.storeManager.getStore().getURIref(id);

            check = this.storeManager.getStore().tuplePattern(uri, null, null);

            i++;
        }

        if(check == null || check.isEmpty())
        {
            Suggest s = new Suggest(uri);
            return s;
        }

        throw new IndividualAlreadyExistsException("Tried to create Suggest with URI " + id + " - but it already exists in the Store");
    }

    public List<Suggest> getByPerson(Person p) {

        List<Suggest> suggestList = new ArrayList<Suggest>();

        List<Map> resultList = this.storeManager.getStore().runSPARQL("SELECT ?suggest ?suggester ?question ?text"
                + " WHERE { ?suggest <" + RDF.TYPE + "> <" + Qbook.getInstance().getSuggestURI() + "> ."
                + " ?suggest <" + RDFS.LABEL + "> ?text ."
                + " ?suggester <" + Qbook.getInstance().getMakesURI() + "> ?suggest ."
                + " ?suggest <" + Qbook.getInstance().getWithPersonURI() + "> <" + p.getUri() + "> ."
                + " ?suggest <" + Qbook.getInstance().getForQuestionURI() + "> ?question "
                + "}");


        for(Map m : resultList)
        {
            Suggest s = new Suggest(((Value) m.get("suggest")).stringValue());
            s.setText(((Value) m.get("text")).stringValue());

            URI suggester = this.storeManager.getStore().getURIref(((Value) m.get("suggester")).stringValue());
            s.setSuggester(this.personService.get(suggester));

            URI question = this.storeManager.getStore().getURIref(((Value) m.get("question")).stringValue());
            s.setQuestion(this.questionService.get(question));

            s.setSuggested(p);

            suggestList.add(s);
        }

        return suggestList;
    }

}
